#include <stdio.h>
#include <time.h>
#include <string.h>
#include <pthread.h>
#include "log.h"

#define LOG_FILE "exDNS.log"

void log_2_file(char *what_happen){
    pthread_rwlock_t rw_lock = PTHREAD_RWLOCK_INITIALIZER;
    char log_content[2048];
    char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    time_t timep;
    struct tm *p;
    time(&timep);
    p=localtime(&timep);

    //printf("%d%d%d ",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
    //printf("%s %d:%d:%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
    sprintf(log_content, "[%d%d%d %s %d:%d:%d]\t%s\n",(1900+p->tm_year), 
            (1+p->tm_mon),
            p->tm_mday, 
            wday[p->tm_wday], 
            p->tm_hour, 
            p->tm_min, 
            p->tm_sec, 
            what_happen);
    //printf(log_content);
    FILE *fp;
    pthread_rwlock_wrlock(&rw_lock);
    fp=fopen(LOG_FILE, "a");
    if(fp==NULL){
        printf("logfile can not open!\n");
    }
    else{
        fwrite(log_content,strlen(log_content), 1, fp);
        //printf("write to file..\n");   
    }
    fclose(fp);
    pthread_rwlock_unlock(&rw_lock);
}
/*
int main(void){
    char a[100];
    sprintf(a, "test!!!");
    log_2_file(a);
    return 0;
}*/
